﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using System.Linq;
using System.Text;
using Snowdreamist.ComponentModel.Plugin;
using Snowdreamist.ComponentModel.Services;

namespace Snowdreamist.ComponentModel.MEF
{
    public class MEFImportPluginManager : ImportedPluginManagerBase
    {
        public MEFImportPluginManager() : this(null) { }

        public MEFImportPluginManager(ExportProvider provider)
        {
            if (provider == null)
                throw new ArgumentNullException("provider");
            this._provider = provider;
        }

        protected override IEnumerable<ImportedPlugin<T, TMetadata>> GetPlugins<T, TMetadata>(ContractMetadata metadata)
        {
            if (metadata == null)
                return null;

            return this._provider.GetExports<T, TMetadata>(metadata.Name).Select(p => new MEFImportedPlugin<T, TMetadata>(p)).ToArray();
        }

        #region private

        private ExportProvider _provider;

        #endregion private
    }
}